//编写一个file客户端程序 对文件管理子服务进行单元测试
//1.编写四个接口

#include <gtest/gtest.h>
#include <gflags/gflags.h>
#include "etcd.hpp"
#include "channel.hpp"
#include <thread>
#include <vector>
#include "utils.hpp"
#include "file.pb.h"
#include "base.pb.h"

DEFINE_bool(run_mode, false, "程序的运行模式, fasle-调试; true-发布;");
DEFINE_string(log_file, "", "发布模式下, 用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下, 用于指定日志输出等级");  //trace default 0

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "这是服务中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(file_service, "/service/file_service", "服务监控目录");

xchat::ServiceChannel::ChannelPtr channel;
std::string single_file_id;

void online(const std::string &service_name, const std::string &service_host)
{
    LOG_DEBUG("上线服务, {}-{}", service_name, service_host);
}
void offline(const std::string &service_name, const std::string &service_host)
{
    LOG_DEBUG("下线服务, {}-{}", service_name, service_host);
}
TEST(put_test, single_test)
{
    //1.读取当前目录下的指定文件数据
    std::string body;
    xchat::readFile("./Makefile",body);
    //2.实例化rpc调用客户端对象 发起rpc 调用
    xchat::FileService_Stub stub(channel.get());
    //构建请求
    xchat::PutSingleFileReq request;
    request.set_request_id("1111");
    request.mutable_file_data()->set_file_name("Makefile");
    request.mutable_file_data()->set_file_size(body.size());
    request.mutable_file_data()->set_file_content(body);
    
    //3.获取响应结果 检测返回值
    brpc::Controller *cntl = new brpc::Controller(); // 防止还没回调 上下文被释放 放堆上
    cntl->set_timeout_ms(5000); // 设置5秒超时
    xchat::PutSingleFileRsp *rsp = new xchat::PutSingleFileRsp();
    stub.PutSingleFile(cntl, &request, rsp, nullptr); // 同步调用
    ASSERT_FALSE(cntl->Failed());
    ASSERT_TRUE(rsp->success());
    ASSERT_EQ(rsp->file_info().file_size(), body.size());
    ASSERT_EQ(rsp->file_info().file_name(), "Makefile");
    single_file_id = rsp->file_info().file_id();
    LOG_DEBUG("文件ID:{}", rsp->file_info().file_id());

}
TEST(get_test, single_test)
{
    //先发起rpc调用 进行rpc 下载
    xchat::FileService_Stub stub(channel.get());
    xchat::GetSingleFileReq request;
    xchat::GetSingleFileRsp *rsp = new xchat::GetSingleFileRsp();
    request.set_request_id("2222");
    request.set_file_id(single_file_id);
    brpc::Controller *cntl = new brpc::Controller(); // 防止还没回调 上下文被释放 放堆上
    stub.GetSingleFile(cntl, &request, rsp, nullptr); // 同步调用
    //将文件数据 存储到文件中
    ASSERT_TRUE(rsp->success());
    ASSERT_FALSE(cntl->Failed());
    ASSERT_EQ(rsp->file_data().file_id(),single_file_id);
    xchat::writeFile("single_file_Makefile_down.txt", rsp->file_data().file_content());

}
std::vector<std::string> multi_file_id;
TEST(put_test, multi_single_test)
{
    //1.读取当前目录下的指定文件数据
    std::string body1;
    xchat::readFile("./base.pb.h",body1);
    std::string body2;
    xchat::readFile("./file.pb.h", body2);
    //2.实例化rpc调用客户端对象 发起rpc 调用
    xchat::FileService_Stub stub(channel.get());
    //构建请求
    xchat::PutMultiFileReq request;
    request.set_request_id("3333");
    auto file_data = request.add_file_data();
    file_data->set_file_name("base.pb.h");
    file_data->set_file_size(body1.size());
    file_data->set_file_content(body1);
    
    auto file_data2 = request.add_file_data();
    file_data2->set_file_name("file.pb.h");
    file_data2->set_file_size(body2.size());
    file_data2->set_file_content(body2);
    
    //3.获取响应结果 检测返回值
    brpc::Controller *cntl = new brpc::Controller(); // 防止还没回调 上下文被释放 放堆上
    xchat::PutMultiFileRsp *rsp = new xchat::PutMultiFileRsp();
    stub.PutMultiFile(cntl, &request, rsp, nullptr); // 同步调用
    ASSERT_FALSE(cntl->Failed());
    ASSERT_TRUE(rsp->success());
    for(int i = 0; i < rsp->file_info_size(); i ++)
    {
        auto info = rsp->file_info(i);
        if(i == 0)
        {
            ASSERT_EQ(info.file_size(), body1.size());
            ASSERT_EQ(info.file_name(), "base.pb.h");
        }
        else if(i == 1)
        {
            ASSERT_EQ(info.file_size(), body2.size());
            ASSERT_EQ(info.file_name(), "file.pb.h");
        }
        multi_file_id.push_back(rsp->file_info(i).file_id());
        LOG_DEBUG("文件ID:{}", rsp->file_info(i).file_id());
    }
}
TEST(get_test, multi_single_test)
{
    //先发起rpc调用 进行rpc 下载
    xchat::FileService_Stub stub(channel.get());
    xchat::GetMultiFileReq request;
    request.set_request_id("4444");
    request.add_file_id_list(multi_file_id[0]);
    request.add_file_id_list(multi_file_id[1]);
    xchat::GetMultiFileRsp *rsp = new xchat::GetMultiFileRsp();
    brpc::Controller *cntl = new brpc::Controller(); // 防止还没回调 上下文被释放 放堆上
    stub.GetMultiFile(cntl, &request, rsp, nullptr); // 同步调用
    //将文件数据 存储到文件中
    ASSERT_TRUE(rsp->success());
    ASSERT_FALSE(cntl->Failed());
    ASSERT_TRUE(rsp->file_data().find(multi_file_id[0]) != rsp->file_data().end());
    ASSERT_TRUE(rsp->file_data().find(multi_file_id[1]) != rsp->file_data().end());
    xchat::writeFile("multi_file_basepb_down.txt", rsp->file_data().find(multi_file_id[0])->second.file_content());
    xchat::writeFile("multi_file_filepb_down.txt", rsp->file_data().find(multi_file_id[1])->second.file_content());

}
int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    google::ParseCommandLineFlags(&argc, &argv, true);
    
    xchat::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    //1.先构造Rpc信道管理对象
    auto service_manager = std::make_shared<xchat::ServiceManager>();
    service_manager->declared(FLAGS_file_service);
    auto put_cb = std::bind(&xchat::ServiceManager::onServiceOnline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&xchat::ServiceManager::onServiceOffline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);
    //2.构造服务发现对象
    xchat::Discovery::ptr dclient = std::make_shared<xchat::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
    
    //3.通过Rpc信道管理对象 获取提供Echo服务器的信道
    channel = service_manager->choose(FLAGS_file_service);
    if (!channel)
    {
        std::cout << "获取信道失败" << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(1));
        abort();
    }
    return RUN_ALL_TESTS();
}